package com.fish.web.controller;

import com.fish.common.annotation.RequiresPermission;
import com.fish.common.result.Result;
import com.fish.model.dto.MenuDTO;
import com.fish.model.vo.MenuVO;
import com.fish.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单Controller
 */
@Tag(name = "菜单管理", description = "菜单相关接口，包括菜单树查询、菜单增删改查等")
@Slf4j
@RestController
@RequestMapping("/api/menu")
public class MenuController {
    
    @Autowired
    private MenuService menuService;
    
    /**
     * 查询所有菜单树
     */
    @Operation(summary = "查询所有菜单树", description = "查询系统中所有菜单的树形结构")
    @GetMapping("/tree")
    public Result<List<MenuVO>> getAllMenuTree() {
        log.info("查询所有菜单树");
        List<MenuVO> menuTree = menuService.getAllMenuTree();
        return Result.success(menuTree);
    }
    
    /**
     * 根据用户ID查询菜单树
     */
    @Operation(summary = "查询用户菜单树", description = "根据用户ID查询该用户有权限访问的菜单树")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1")
    @GetMapping("/user/{userId}")
    public Result<List<MenuVO>> getMenuTreeByUserId(@PathVariable Long userId) {
        log.info("查询用户菜单树，用户ID：{}", userId);
        List<MenuVO> menuTree = menuService.getMenuTreeByUserId(userId);
        return Result.success(menuTree);
    }
    
    /**
     * 添加菜单
     */
    @Operation(summary = "添加菜单", description = "创建新的菜单项")
    @PostMapping("/add")
    @RequiresPermission("system:menu:add")
    public Result<MenuVO> addMenu(@Validated @RequestBody MenuDTO menuDTO) {
        log.info("添加菜单，菜单名称：{}", menuDTO.getMenuName());
        MenuVO menuVO = menuService.addMenu(menuDTO);
        return Result.success("添加成功", menuVO);
    }
    
    /**
     * 更新菜单
     */
    @Operation(summary = "更新菜单", description = "更新菜单信息")
    @PutMapping("/update")
    @RequiresPermission("system:menu:edit")
    public Result<MenuVO> updateMenu(@Validated @RequestBody MenuDTO menuDTO) {
        log.info("更新菜单，菜单ID：{}", menuDTO.getId());
        MenuVO menuVO = menuService.updateMenu(menuDTO);
        return Result.success("更新成功", menuVO);
    }
    
    /**
     * 删除菜单
     */
    @Operation(summary = "删除菜单", description = "根据菜单ID删除菜单")
    @Parameter(name = "menuId", description = "菜单ID", required = true, example = "1")
    @DeleteMapping("/delete/{menuId}")
    @RequiresPermission("system:menu:delete")
    public Result<Void> deleteMenu(@PathVariable Long menuId) {
        log.info("删除菜单，菜单ID：{}", menuId);
        menuService.deleteMenu(menuId);
        return Result.success("删除成功", null);
    }
}